<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <div class="form-item">
          <span class="title">活动标题</span>
          <el-input placeholder="请输入名称" v-model="param.lotteryTurntable.name"></el-input>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item">
          <span class="title" style="width:120px">创建日期</span>
          <el-date-picker
            v-model="createdateList"
            type="daterange"
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :clearable="false"
          ></el-date-picker>
        </div>
      </el-col>
      <el-col :span="6">
        <div class="form-item button-container">
          <el-button type="primary" @click="search" icon="el-icon-search">查询</el-button>
          <el-button type="primary" @click="resetSearchCotnet" icon="el-icon-refresh">重置</el-button>
          <el-button type="primary" @click="addLuck" icon="el-icon-circle-plus-outline" v-if="isPermissionsBtn('luck_add')">新增</el-button>
        </div>
      </el-col>
    </el-row>
    <div class="table-body">
      <el-table :data="tableData" v-loading="isLoading" style="width: 100%" border>
        <el-table-column prop="name" label="转盘标题"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="userName" label="创建人"></el-table-column>
        <el-table-column fixed="right" label="操作" width="250">
          <template slot-scope="scope">
            <el-button
              @click="handleClick(scope.row)"
              size="small"
              v-if="isPermissionsBtn('luck_open')"
            >{{scope.row.status == 0 ? '启用' : '取消启用'}}</el-button>
            <el-button @click="handleToDetail(scope.row)" type="primary" size="small" v-if="isPermissionsBtn('luck_detail')">详情</el-button>
            <el-popconfirm title="确定删除此活动？" @onConfirm="handleToDelete(scope.row)">
              <el-button type="danger" style="margin-left:15px" size="small" slot="reference" v-if="isPermissionsBtn('luck_delete')">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @current-change="currentPageChange"
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
  name: "",
  data() {
    return {
      param: {
        lotteryTurntable: {
          name: "", // 名称
          startTime: "", // 创建开始日期
          endTime: "" // 创建开始日期
        },
        queryRequest: {
          pageNum: 1,
          pageSize: 10
        }
      },
      createdateList: [],
      isLoading: false,
      tableData: [],
      total: 1
    };
  },
  components: {},
  computed: {
    ...mapGetters(["isPermissionsBtn"])
  },
  beforeMount() {},
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      this.isLoading = true;
      if (this.createdateList.length > 0) {
        this.param.lotteryTurntable.startTime = this.createdateList[0];
        this.param.lotteryTurntable.endTime = this.createdateList[1];
      }
      this.$service.findLotteryList(this.param).then(res => {
        this.isLoading = false;
        if (res.code == "00000") {
          if (
            res.data.records.length == 0 &&
            this.param.queryRequest.pageNum != 1
          ) {
            this.param.queryRequest.pageNum -= 1;
            this.initData();
          }
          this.tableData = (res.data && res.data.records) || [];
          this.total = res.data.total || 0;
        }
      });
    },
    search() {
      this.param.queryRequest.pageNum = 1;
      this.initData();
    },
    resetSearchCotnet() {
      //重置搜索条件
      this.param.lotteryTurntable.name = "";
      this.param.lotteryTurntable.startTime = "";
      this.param.lotteryTurntable.endTime = "";
      this.createdateList = [];
    },
    addLuck() {
      this.$router.push({
        path: "/luckActivityAdd"
      });
    },
    handleClick(scope) {
      if (scope.status == 0) {
        this.$service.lotteryTurntableOpenStatus(scope.id).then(res => {
          if (res.code == "00000") {
            this.$message({
              message: "开启成功",
              type: "success"
            });

            this.initData();
          } else {
            this.$message({
              message: res.message,
              type: "warning"
            });
          }
        });
      } else {
        this.$service.lotteryTurntableCloseStatus(scope.id).then(res => {
          if (res.code == "00000") {
            this.$message({
              message: "取消开启成功",
              type: "success"
            });

            this.initData();
          } else {
            this.$message({
              message: res.message,
              type: "warning"
            });
          }
        });
      }
    },
    handleToDetail(scope) {
      this.$router.push({
        path: "/luckActivityDetail",
        query: { id: scope.id }
      });
    },
    handleToDelete(scope) {
      this.$service.delLottery(scope.id).then(res => {
        if (res.code == "00000") {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        } else {
          this.$message({
            message: res.message,
            type: "warning"
          });
        }
      });
    },
    currentPageChange(e) {
      this.param.queryRequest.pageNum = e;
      this.initData();
    }
  },
  watch: {}
};
</script>
<style scoped>
</style>
